package stu.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import stu.aistar.model.pojo.Movie;
import stu.aistar.model.pojo.MovieType;
import stu.aistar.model.vo.DataJsonVo;
import stu.aistar.model.vo.Result;
import stu.aistar.service.MovieTypeService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/types")
public class MovieTypeController {

    @Autowired
    private MovieTypeService movieTypeService;

    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/all")
    @ResponseBody
    public Map<String,Object> findAll(HttpServletRequest request){
        String page = request.getParameter("page");
        String pageSize = request.getParameter("limit");
        PageInfo<MovieType> types = movieTypeService.findByPage(Integer.parseInt(page),Integer.parseInt(pageSize));
        Integer total = movieTypeService.findTotal();
        DataJsonVo vo = new DataJsonVo();

        Map<String,Object> map = new HashMap<>();
        map.put("msg","查询成功");
        map.put("code",0);
        map.put("count",total);
        map.put("data",types.getList());
        return map;
    }

    @RequestMapping("/isExist")
    @ResponseBody
    public Result findMovieType(@RequestParam("typeName") String name){
        Result result;
        MovieType type = movieTypeService.findMovieTypeByName(name);
        if(null==type){
            result = new Result("200","电影名不存在",null);
        }else {
            result = new Result("400","电影名已存在",type.getId());
        }
        return result;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result saveMovie(@RequestParam("typeName") String name,HttpServletRequest request){
        Result result;
        Integer row = movieTypeService.insertMovieType(name);
        if(row == 1){
            result = new Result("200","添加成功",null);
            ListOperations<String,MovieType> listOperations=redisTemplate.opsForList();
            List<MovieType> types = movieTypeService.findAll();
            redisTemplate.delete("movieTypes");
            for (MovieType type: types) {
                listOperations.leftPush("movieTypes",type);
            }
            request.getSession().setAttribute("movieTypes",types);
        }else {
            result = new Result("500","添加失败",null);
        }
        return result;
    }

    @PostMapping("/editType")
    @ResponseBody
    public Result editType(@RequestParam("typeName") String name,@RequestParam("id") Integer id,HttpServletRequest request){
       MovieType movieType = new MovieType(id,name,null,null);
        Result result = null;
        Boolean flag = movieTypeService.updateTypeById(movieType);
        if(flag){
            result = new Result("200","修改成功",null);
            ListOperations<String,MovieType> listOperations=redisTemplate.opsForList();
            List<MovieType> types = movieTypeService.findAll();
            redisTemplate.delete("movieTypes");
            for (MovieType type: types) {
                listOperations.leftPush("movieTypes",type);
            }
            request.getSession().setAttribute("movieTypes",types);
        }else {
            result = new Result("500","修改失败",null);
        }
        return result;
    }

    @RequestMapping("/deleteType")
    @ResponseBody
    public Result deleteType(@RequestParam("typeName") String typeName,HttpServletRequest request){
        Result result = null;
        Boolean flag = movieTypeService.deleteTypeByName(typeName);
        if(flag){
            result = new Result("200","删除成功",null);
            ListOperations<String,MovieType> listOperations = redisTemplate.opsForList();
            List<MovieType> types = movieTypeService.findAll();
            redisTemplate.delete("movieTypes");
            for (MovieType type: types) {
                listOperations.leftPush("movieTypes",type);
            }
            request.getSession().setAttribute("movieTypes",types);
        }else {
            result = new Result("500","删除失败",null);
        }
        return result;
    }

}
